﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using System.Web.Security;
using Avatar.Model;
using AvatarHelper;
using System.Data.Linq;
using Avatar.MembershipService;

public partial class PersonInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dictionary<string, int> _year = new Dictionary<string, int>();
            for (int _i = 2001; _i > 1940; _i--)
            {
                _year.Add(_i.ToString(), _i);
            }
            ddl_Year.DataSource = _year;
            ddl_Year.DataTextField = "Key";
            ddl_Year.DataValueField = "Value";
            ddl_Year.DataBind();

            Dictionary<string, int> _month = new Dictionary<string, int>();
            for (int _i = 1; _i < 13; _i++)
            {
                _month.Add(_i.ToString(), _i);
            }
            ddl_Month.DataSource = _month;
            ddl_Month.DataTextField = "key";
            ddl_Month.DataValueField = "Value";
            ddl_Month.DataBind();

            Dictionary<string, int> _day = new Dictionary<string, int>();
            for (int _i = 1; _i < 32; _i++)
            {
                _day.Add(_i.ToString(), _i);
            }
            ddl_Day.DataSource = _day;
            ddl_Day.DataTextField = "key";
            ddl_Day.DataValueField = "Value";
            ddl_Day.DataBind();

            var _user = Membership.GetUser();
            if (_user != null)
            {
                EmailTextBox.Text = _user.UserName;
                EmailTextBox.Enabled = false;
                using (var _adc = DBHelper.GetDataContext())
                {
                    DropDownListHelper.Bind(ddl_Sex, _adc.Sex.ToList(), "name", "ID");
                    DropDownListHelper.Bind(ddl_Diploma, _adc.Diploma.ToList(), "name", "ID");
                    DropDownListHelper.Bind(ddl_WorkingAge, _adc.WorkingAge.ToList(), "age", "ID");
                    DropDownListHelper.Bind(ddl_Marry, _adc.MaritalStatus.ToList(), "Status", "ID");
                    DropDownListHelper.Bind(ddl_DocumentType, _adc.DocumentType.ToList(), "Document", "ID");
                    DropDownListHelper.Bind(ddl_Salary, _adc.SalaryYear.ToList(), "Salary", "ID");
                    var _resume = _adc.Resume.Where(r => r.User == _user.UserName).FirstOrDefault();

                    if (_resume != null)
                    {
                        var _BasicInfo = _resume.PersonalBasicInfo;
                        if (_BasicInfo != null)
                        {
                            NameTextBox.Text = _BasicInfo.Name;
                            ddl_Year.SelectedValue = _BasicInfo.Birthday.Year.ToString();
                            ddl_Month.SelectedValue = _BasicInfo.Birthday.Month.ToString();
                            ddl_Day.SelectedValue = _BasicInfo.Birthday.Day.ToString();
                            ddl_WorkingAge.SelectedValue = _BasicInfo.WorkingAgeID.ToString();

                            ddl_DocumentType.SelectedValue = _BasicInfo.DocumentTypeID.ToString();
                            DocumentNumberTextBox.Text = _BasicInfo.DocumentNumber;
                            
                            ddl_Salary.SelectedValue = _BasicInfo.SalaryID.ToString();
                            TelNumberTextBox.Text = _BasicInfo.TelNumber;
                            HeightTextBox.Text = _BasicInfo.Height.ToString();
                            KeyWordTextBox.Text = _BasicInfo.KeyWord;
                            ddl_Marry.SelectedValue = _BasicInfo.MaritalStatusID.ToString();
                            QQTextBox.Text = _BasicInfo.QQ;
                            txt_Blog.Text = _BasicInfo.Blog;
                            Location1.City = _BasicInfo.AccountLocation;
                            Location2.City = _BasicInfo.LivingCity;
                            ddl_Sex.SelectedValue = _BasicInfo.SexID.ToString();
                            ddl_Diploma.SelectedValue = _BasicInfo.DiplomaID.ToString();
                        }
                    }
                }
            }
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            LoginManager.PersonAccessCheck();

            string _userName = Membership.GetUser().UserName;
            DateTime _birthday = Convert.ToDateTime(ddl_Year.SelectedValue + "/" + ddl_Month.SelectedValue + "/" + ddl_Day.SelectedValue);

            BasicInfoManager.BasicInfoSave(
                _userName,
                Location1.City,
                _birthday,
                txt_Blog.Text,
                DocumentNumberTextBox.Text,
                Convert.ToInt32(ddl_DocumentType.SelectedValue),
                string.IsNullOrEmpty(HeightTextBox.Text) ? 0 : Convert.ToInt32(HeightTextBox.Text),
                KeyWordTextBox.Text,
                Location2.City,
                Convert.ToInt32(ddl_Marry.SelectedValue),
                NameTextBox.Text,
                QQTextBox.Text,
                Convert.ToInt32(ddl_Salary.SelectedValue),
                TelNumberTextBox.Text,
                Convert.ToInt32(ddl_WorkingAge.SelectedValue),
                Convert.ToInt32(ddl_Diploma.SelectedValue),
                Convert.ToInt32(ddl_Sex.SelectedValue)
                );

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
